<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/14
 * Time: 15:33
 */

namespace app\modules\api\models;

use app\models\Setting;
use app\models\Share;
use app\models\User;
use app\behaviors\OrderBehavior;

class BindForm extends ApiModel
{
    public $store_id;
    public $user_id;

    /**
     * @return array
     * 绑定上下级关系
     */
    public function save()
    {
        $setting = Setting::findOne(['store_id' => $this->store_id]);
        $parent_id = \Yii::$app->request->get('parent_id');

        if(strpos($parent_id,'team_id') !== false){
            $arr = explode("-",$parent_id);
            $shop_id = $arr[0];
            $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0]);
            $user->shop_id = $shop_id;
            $user->save();
            return [
                'code' => 0,
                'msg' => '团长绑定成功!'
            ];

        }

        //店铺未开启分销
        if ($setting->level == 0) {
            return [
                'code' => 1,
                'msg' => '未开启分销'
            ];
        }
        //自身分享的页面
        if ($this->user_id == $parent_id) {
            return [
                'code' => 1,
                'msg' => '自身分销的页面'
            ];
        }
        //父级id是否是分销商
        $exit = Share::find()->andWhere(['user_id' => $parent_id, 'store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])->exists();
        if (!$exit) {
            return [
                'code' => 1,
                'msg' => '不是分销商'
            ];
        }
        //判断父级id是否在三级分销链中
        $res = self::check($this->user_id, $parent_id, 0);
        if ($res['code'] == 1) {
            return [
                'code' => 1,
                'msg' => ''
            ];
        }
        $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0]);
        $parent = User::findOne(['id' => $parent_id, 'is_delete' => 0]);
        if ($parent->is_distributor != 1) {
            return [
                'code' => 1,
                'msg' => ''
            ];
        }
        //用户是否是分销商
        if ($user->is_distributor == 1) {
            return [
                'code' => 1,
                'msg' => ''
            ];
        }
        //用户是否存在父级
        if ($user->parent_id != 0) {
            return [
                'code' => 1,
                'msg' => ''
            ];
        }
        $user->parent_id = $parent_id;
        if ($user->save()) {
            file_put_contents("/tmp/share.log",$setting->first.PHP_EOL,8);
            if (!$setting->first)  return ['code' => 0, 'msg' => '成功', 'data' => $parent->nickname];

            //发放佣金
            $OrderBehavior = new OrderBehavior();
            $OrderBehavior->send_share_money($setting->first,$parent_id,$user->id,1);

            return [
                'code' => 0,
                'msg' => '成功',
                'data' => $parent->nickname
            ];
        } else {
            return [
                'code' => 1,
                'msg' => '网络异常'
            ];
        }
    }

    public static function check($user_id, $parent_id, $root = 0)
    {
        if ($root == 3) {
            return [
                'code' => 0,
                'msg' => '可以绑定'
            ];
        }
        if ($parent_id == 0) {
            return [
                'code' => 0,
                'msg' => '可以绑定'
            ];
        }
        $user = User::findOne(['id' => $parent_id, 'is_delete' => 0]);
        if ($user_id == $user->parent_id) {
            return [
                'code' => 1,
                'msg' => '不能'
            ];
        }
        return self::check($user_id, $user->parent_id, $root + 1);
    }
}
